extends Node
signal new_round_begin
signal round_end

var characters:Array[CharacterBody2D]
var battle_queue:Array[Dictionary]
var left_side_characters:Array[CharacterBody2D]
var right_side_characters:Array[CharacterBody2D]
var current_round_number:int = 0
var cur_operating_player:CharacterBody2D = null
var cur_operating_index:int = 0
var cur_action_character_index:int = 0 #当前是哪个角色在动
var cur_selected_character:CharacterBody2D
var cur_round = 1

func character_bind_signal():
	for charater in characters:
		charater.choosing_finished.connect(_on_choosing_finished)
		charater.turn_end.connect(_on_turn_end)
		charater.character_selected.connect(_on_character_selected)

# 开始进入战斗画面，需要分敌我双方
func start_battle():
	cur_round = 1
	character_bind_signal()
	character_grouping()
	sort_battle_quene()
	right_start_choosing()
	left_start_choosing()

# 左边是玩家，一个个执行操作
func left_start_choosing():
	print("left_start_choosing,cur_operating_index="+str(cur_operating_index))
	cur_operating_index= 0
	cur_operating_player = left_side_characters[0]
	cur_operating_player.start_choosing()

# 右边是敌人，随机获取一个主动技能并随机施展给随机一个角色
func right_start_choosing():
	for character in right_side_characters:
		var skill = get_random_skill(character.skills)
		var index = randi_range(0,left_side_characters.size()-1)
		battle_queue.append({"me":character,"targets":[left_side_characters[index]],"skill":skill})
		
# 随机获取一个主动技能
func get_random_skill(_skills:Array[Skill]) -> Skill:
	var index = randi_range(0,_skills.size()-1)
	if _skills[index].triggle != Skill.ON_USE:
		return get_random_skill(_skills)
	else:
		return _skills[index]
# 将所有参战的角色分到敌我各自的分组	
func character_grouping():
	print("character_grouping")
	for character in characters:
		if character.side == Character.LEFT:
			left_side_characters.append(character)
		else:
			right_side_characters.append(character)
# 根据速度排除战斗序列
func sort_battle_quene():
	print("sort_battle_quene")
	battle_queue.sort_custom(func(a,b):
		if a.me.speed >= b.me.speed:
			return true
		return false	
	)
# 开始战斗
func start_turn():
	await get_tree().create_timer(0.4).timeout
	print("start turn:",cur_action_character_index)
	if cur_action_character_index >= battle_queue.size():
		cur_action_character_index = 0
		start_new_round()
		emit_signal("round_end",cur_round)
	else:
		var me = battle_queue[cur_action_character_index].me
		var tars = battle_queue[cur_action_character_index].targets
		me.take_turn(tars,battle_queue[cur_action_character_index].skill)
		cur_action_character_index += 1
	
func start_new_round():
	cur_round += 1
	print("start new round:",cur_round)
	battle_queue.clear()
	right_start_choosing()
	left_start_choosing()

func _on_choosing_finished():
	var array:Array[CharacterBody2D] = []
	array.append(cur_selected_character)
	battle_queue.append({"me":cur_operating_player,"targets":array,"skill":cur_operating_player.cur_selected_skill})
	print("_on_choosing_finished,battle_queue=",battle_queue)
	cur_operating_index += 1
	if cur_operating_index < left_side_characters.size():
		cur_operating_player = left_side_characters[cur_operating_index]
		cur_operating_player.start_choosing()
	else:
		# 所有玩家角色都操作完成
		sort_battle_quene()
		cur_action_character_index = 0
		emit_signal("new_round_begin")
		start_turn()
	pass

func _on_character_selected(_character:CharacterBody2D):
	if cur_operating_player.cur_selected_skill != null:
		cur_selected_character = _character
		cur_operating_player.end_of_choice()

func _on_turn_end():
	start_turn()
